<template>
  <div class="design-attribute">
    <!-- 配置导航 -->
    <div class="header">
      <div class="header-active" :style="{ left: offset }" />
      <div class="header-nav">
        <div
          class="nav-label"
          v-for="(item, index) in nav"
          :key="index"
          @click="handle(item)"
        >
          <span>{{ item.label }}</span>
        </div>
      </div>
    </div>
    <div class="design-panel">
      <div class="attribute panel" v-if="key == 0">
        <!-- 页面配置 -->
        <div class="shape-panel">
          <div class="complex-position-box">
            <div class="position-item page-config">
              <span class="label">宽度</span>
              <text-number v-model="page.width" />
            </div>
            <div class="position-item page-config">
              <span class="label">高度</span>
              <text-number v-model="page.height" />
            </div>
            <div class="position-item page-config">
              <span class="label">行数</span>
              <text-number v-model="page.row" />
            </div>
            <div class="position-item page-config">
              <span class="label">缩放</span>
              <el-input
                placeholder="请输入内容"
                v-model="scaleFix"
                :disabled="true"
              >
              </el-input>
            </div>
            <div class="position-item page-config">
              <span class="label">列数</span>
              <text-number v-model="page.col" />
            </div>
            <!-- <div class="position-item page-config">
              <span class="label">间隔</span>
              <text-number v-model="page.layout.gutter" />
            </div> -->
          </div>
        </div>
        <!-- 组件位置信息 -->
        <div class="shape-panel" v-if="complex">
          <div class="complex-label-box" v-if="config">
            {{ config.label }}
          </div>
          <div class="complex-position-box">
            <div
              class="position-item position-complex-config"
              v-for="(item, index) in position"
              :key="index"
            >
              <span class="label">{{ item.label }}</span>
              <text-number
                v-model="complex[item.key]"
                @change="(data) => changeConfig(data, item.key)"
              />
              <!-- <span>{{ complex ? parseInt(complex[item.key]) : 0 }}</span> -->
            </div>
          </div>
        </div>
        <!-- 属性配置项 -->
        <div class="complex-config">
          <div class="attr-list" v-if="complex">
            <panel-collapse
              v-for="(group, index) in attrGroup"
              :key="index"
              :option="group"
              :count="count"
            >
              <div
                class="attr-item"
                v-for="(item, index) in group.child"
                :key="index"
              >
                <span
                  class="attr-label"
                  v-if="!item.hidden || item.hidden == null"
                  >{{ item.title }}</span
                >
                <div class="attr-content">
                  <component
                    :is="item.formType"
                    :value.sync="item.data"
                    :type="item.key"
                    @change="changeData"
                    @update="updateData"
                  />
                </div>
              </div>
            </panel-collapse>
          </div>
        </div>
      </div>
      <!-- 数据来源 -->
      <div class="data-source panel" v-if="key == 1">
        <div class="data-source-item">
          <span class="souce-label">数据类型</span>
          <div class="source-content">
            <el-select v-model="dataType" placeholder="请选择">
              <el-option
                v-for="item in dataTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="data-source-item">
          <span class="souce-label">接口地址</span>
          <div class="source-content">
            <el-input placeholder="请输入接口地址" v-model="apiInput">
            </el-input>
          </div>
        </div>
        <div class="data-source-item">
          <span class="souce-label">请求方式</span>
          <div class="source-content">
            <el-select v-model="requestVal" placeholder="请选择">
              <el-option
                v-for="item in requsetMode"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <el-button class="data-source-btn">接口请求</el-button>
      </div>
      <!-- 事件 -->
      <div class="active-bind data-source panel" v-if="key == 2">
        <div class="data-source-item">
          <span class="souce-label">绑定组件</span>
          <div class="source-content">
            <el-select v-model="bindingID" placeholder="请选择">
              <el-option
                v-for="item in list"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="data-source-item">
          <span class="souce-label">参数名称</span>
          <div class="source-content">
            <el-input placeholder="请输入参数名称" v-model="apiInput">
            </el-input>
          </div>
        </div>
        <div class="data-source-item">
          <span class="souce-label">映射字段</span>
          <div class="source-content">
            <el-input placeholder="请输入参数名称" v-model="apiInput">
            </el-input>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import attrcomponent from "./components/index";
import select from "@/components/select-option";
import input from "@/components/input-option";
import panel from "@/components/panel-collapse";
export default {
  components: {
    ...attrcomponent,
    "select-option": select,
    "input-option": input,
    "panel-collapse": panel,
  },
  props: {
    page: {
      type: Object,
      default() {
        return {
          width: 1920, // 宽度
          height: 1080, // 高度
          left: 0,
          top: 0,
          layout: {
            row: 4,
            column: 4,
            gutter: 6,
          },
          row: 1,
          col: 1,
          scale: 0.5,
        };
      },
    },
    selectComponents: {
      type: Object,
    },
  },
  data() {
    return {
      count: 0,
      key: 0,
      nav: [
        {
          key: 0,
          label: "基础属性",
        },
        {
          key: 1,
          label: "数据来源",
        },
        {
          key: 2,
          label: "交互事件",
        },
      ],
      // 位置信息
      position: [
        {
          label: "W",
          key: "w",
        },
        {
          label: "H",
          key: "h",
        },
        {
          label: "X",
          key: "x",
        },
        {
          label: "Y",
          key: "y",
        },
        {
          label: "R",
          key: "r",
        },
        {
          label: "Z",
          key: "z",
        },
      ],
      eventList: [
        {
          value: "onclick",
          label: "单击",
        },
        {
          value: "dblclick",
          label: "双击",
        },
      ],
      option: {
        value: "id",
        label: "label",
      },
      attrList: [],
      attroption: {
        value: "key",
        label: "title",
      },
      //数据类型
      dataTypeOptions: [
        {
          value: "defaulteVal",
          label: "静态数据",
        },
        {
          value: "api",
          label: "API接口数据",
        },
      ],
      dataType: "",
      apiInput: "", //接口地址
      //请求方式
      requsetMode: [
        {
          value: "GET",
          label: "get",
        },
        {
          value: "POST",
          label: "post",
        },
        {
          value: "DELETE",
          label: "delete",
        },
        {
          value: "PUT",
          label: "put",
        },
      ],
      requestVal: "",
      //绑定组件ID
      bindingID: "",
    };
  },
  computed: {
    list() {
      return this.$store.state.design.complexData;
    },
    offset() {
      return `${this.key * 69 + 16}px`;
    },
    config() {
      // const complex =JSON.parse(JSON.stringify(this.$store.state.design.selectComplex)) ;
      const complex = this.selectComponents;
      return complex ? complex.config : null;
    },
    configAttr() {
      // const config = this.$store.state.design.selectComplex.config;
      const config = this.selectComponents.config;
      return config ? config.attr : [];
    },
    attrGroup() {
      return this.group();
    },
    configActive() {
      // const config = this.$store.state.design.selectComplex.config;
      const config = this.selectComponents.config;
      return config ? config.active : [];
    },
    complex() {
      return this.layoutSpan();
    },
    scaleFix() {
      return this.page.scale.toFixed(2);
    },
  },

  methods: {
    change(data) {
      console.log(data, "wwww");
    },
    group() {
      // 属性分组
      const attr = this.selectComponents.config.attr;
      if (!attr) return;
      const group = this.$store.state.design.group;
      group.map((item) => {
        const child = attr.filter((e) => item.key === e.group);
        item.child = child;
      });
      this.count++;
      return group;
    },
    attr(data) {
      // 获取组件的全部属性
      this.attrList = data.config.attr;
    },
    handle(item) {
      this.key = item.key;
    },
    deleteEvent(index) {
      // 删除交互事件
      this.config.active.splice(index, 1);
    },
    createEvent() {
      // 新建交互事件
      this.config.active.push({
        bindId: null, // 绑定操作组件
        event: null,
        reslut: null, // 操作事件属性
        value: null,
        params: null,
        describe: "事件描述",
      });
    },
    changeConfig(data, key) {
      console.log(data, key);
    },
    // 修改属性
    changeData(data) {
      const { key, value } = data;
      this.config.attr.map((item) => {
        if (item.key == key) {
          item.data = value;
        }
        return item;
      });
      this.selectComponents.config.attr.map((item) => {
        if (item.key == key) {
          item.data = value;
        }
        return item;
      });
      this.selectComponents.count++;
    },
    // 修改组件宽高 栅格布局化
    layoutSpan() {
      if (this.selectComponents) {
        if (this.selectComponents.config.attr) {
          const attr = this.selectComponents.config.attr;
          attr.forEach((ele) => {
            if (ele.formType == "layout-span") {
              if (ele.data.status) {
                const width = (1920 / this.page.col) * ele.data.column;
                const height = (1080 / this.page.row) * ele.data.row;
                this.selectComponents.w = width;
                this.selectComponents.h = height;
              }
            }
          });
        }
      }
      return this.selectComponents;
    },
    updateData(data) {
      if (this.page.col > 1 && this.page.row > 1) {
        const width = (1920 / this.page.col) * data.column;
        const height = (1080 / this.page.row) * data.row;
        this.complex.w = width;
        this.complex.h = height;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
$bgColor: #252626;
$color: var(--font-color);
$inputColor: #010101;
::v-deep {
  .el-input-number {
    width: 100% !important;
    &:hover {
      .el-input-number__decrease,
      .el-input-number__increase {
        display: block;
      }
    }

    .el-input-number__decrease,
    .el-input-number__increase {
      display: none;
      width: 18px !important;
    }
    // .el-input-number__decrease:hover, .el-input-number__increase:hover{
    //   color:#DCDFE6
    // }

    .el-input__inner {
      padding-left: 0 !important;
      padding-right: 18px !important;
    }
  }
  .el-input-number--small {
    line-height: 28px;
  }
  .el-input__inner {
    height: 28px;
    line-height: 28px;
  }
  .el-input__inner:hover {
    border: 1px solid #dcdfe6;
  }
  .el-select {
    .el-input__icon {
      line-height: 28px;
    }
  }
}

.data-source {
  height: 100%;
  padding: 6px 10px;
  .data-source-item {
    width: 100%;
    min-height: 40px;
    display: flex;
    justify-items: flex-start;
    align-items: center;
    .souce-label {
      margin-right: 6px;
      height: 24px;
      line-height: 24px;
      min-width: 69px;
      font-size: 14px;
      text-align: right;
      color:var(--font-color)
    }
    .source-content {
      flex: 1;
    }
  }
  .data-source-btn {
    margin-top: 20px;
    margin-left: 50%;
    transform: translateX(-50%);
  }
}

.active-bind {
  width: 100%;
  height: 100%;
  padding: 0 6px;

  .active-create {
    width: 100%;
    padding: 4px;
    background-color: #409eff;
    border-radius: 2px;
    cursor: pointer;
    font-size: 12px;
    color:var(--font-color);
    text-align: center;
    margin: 12px auto;
  }

  .active-content {
    width: 100%;
    height: calc(100% - 60px);
    overflow-y: auto;
    overflow-x: hidden;

    &::-webkit-scrollbar {
      display: none;
    }
  }

  .active-item {
    width: 100%;
    padding: 4px 6px;
    background-color: rgb(247, 247, 247);
    margin-bottom: 6px;
    border-radius: 2px;
    transition: all 0.2s ease-in-out 0s;

    .active-title {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 13px;
      padding: 6px 0;
      border-bottom: 1px solid rgb(219, 219, 219);

      .svg-icon {
        width: 20px;
        cursor: pointer;

        &:hover {
          color: #409eff;
        }
      }
    }

    .active-form {
      width: 100%;
      height: 32px;
      font-size: 12px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin: 6px auto 4px;
    }

    .content {
      flex: 1;
      margin-left: 6px;
    }

    .active-label {
      text-align: right;
    }
  }
}
</style>

<style lang="scss" scoped>
.design-attribute {
  width: 100%;
  height: 100%;

  .header {
    position: relative;
    width: 100%;
    height: 36px;
    padding: 0 16px;
    border-bottom: 1px solid rgb(219, 219, 219);

    .header-active {
      position: absolute;
      left: 207px;
      bottom: 0px;
      width: 30%;
      border-bottom: 2px solid var(--tabNav-background);
      transition: all 0.28s ease;
    }

    .header-nav {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;

      .nav-label {
        flex: 1;
        color: var(--font-color);
        text-align: center;
        font-size: 14px;
        cursor: pointer;
      }
    }
  }

  .design-panel {
    width: 100%;
    height: calc(100% - 36px);

    .panel {
      width: 100%;
    }

    .attribute {
      width: 100%;
      height: 100%;
    }
  }
}

.complex-label-box {
  width: 100%;
  padding: 6px 16px;
  display: flex;
  border-bottom: 1px solid rgb(219, 219, 219);
  color: var(--font-color);
}

.complex-position-box {
  width: 100%;
  padding: 6px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  border-bottom: 1px solid rgb(219, 219, 219);

  .position-item {
    height: 20px;
    line-height: 20px;
    font-size: 12px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin: 6px 0;
  }

  .page-config {
    width: 50%;
  }

  .page-config .label {
    display: inline;
    text-align: center;
    min-width: 36px;
    color:var(--font-color)
  }

  .position-complex-config {
    width: 50%;
  }

  .position-complex-config .label {
    display: inline;
    width: 36px;
    text-align: center;
  }
}
</style>

<style scoped lang="scss">
.complex-config {
  width: 100%;
  height: calc(100% - 251px);
  overflow-y: auto;
  overflow-x: hidden;
}

.attr-list {
  width: 100%;

  .attr-item {
    width: 100%;
    min-height: 40px;
    display: flex;
    justify-items: flex-start;
    align-items: center;
    color: var(--font-color);

    .attr-label {
      margin-right: 6px;
      height: 24px;
      line-height: 24px;
      min-width: 69px;
      font-size: 12px;
      text-align: right;
      color: var(--font-color);
    }

    .attr-content {
      flex: 1;
      margin-right: 6px;
    }
  }
}
</style>
